{% extends "questions/base.html" %}
{% from "dashboards/includes/macros.html" import topic_selector %}

{% set classes = 'questions' %}

{% if all_products %}
  {% set product_title = _('All Products') %}
{% elif multiple_products %}
  {% set product_title = _('Multiple Products') %}
{% else %}
  {% set product_title = pgettext('DB: products.Product.title', products[0].title) %}
{% endif %}

{% set title = _('{product} Support Forum')|f(product=product_title) %}

{% set crumbs = [(url('questions.home'), _('Support Forum')), (None, product_title)] %}

{% set canonical_url = canonicalize(viewname='questions.list', product_slug=product_slug)|urlparams(None, request.GET) %}
{% if questions.number > 1 %}
  {% set canonical_url = canonical_url|urlparams(page=questions.number) %}
{% endif %}

{% set meta = (('robots', 'noindex'),) %}

{# This takes kwargs, even though it doesn't say so, because it uses them.
   It's a Jinja Magic thing. #}
{% macro questions_url() %}
  {{ url('questions.list', product_slug)|urlparams(None, request.GET, **kwargs) }}
{% endmacro %}

{% block content %}
  <div class="grid_12">
    {% if topic_list %}
      {{ topic_selector(topic_list, topic) }}
    {% endif %}

    <h1>{{ title }}</h1>

    <div id="recent-stats" class="cf">
        <div class="total">
          <h3>{{ _('Total questions: <strong>{num}</strong>')|fe(num=recent_asked_count) }}</h3>
        </div>
        <div class="no-reply">
          <h3>
            {% if recent_unanswered_count > 0 %}
              {% trans recent_unanswered_count, url=questions_url(filter='recently-unanswered', show='all') %}
                <strong>{{recent_unanswered_count}}</strong> question in the last <strong>24 hours</strong> has no reply.
                <a href="{{url}}">Help solve it!</a>
                {% pluralize recent_unanswered_count %}
                <strong>{{recent_unanswered_count}}</strong> questions in the last <strong>24 hours</strong> have no reply.
                <a href="{{url}}">Help solve them!</a>
              {% endtrans %}
            {% else %}
              {% trans %}
                <strong>0</strong> questions in the last <strong>24 hours</strong> have no reply. <strong>Good job!</strong>
              {% endtrans %}
            {% endif %}
          </h3>
        </div>
        <div class="progress-bar-percent">{{ _('Progress:') }} <strong>{{ recent_answered_percent }}%</strong></div>
        <div class="progress-bar {% if recent_answered_percent == 100 %} complete{% endif %}">
          <div style="width: {{ recent_answered_percent }}%;"></div>
        </div>
      </div>
      {% if tags %}
        <div id="tagged">
          {{ _('Showing questions tagged:') }}
            <ul>
          {% for tag in tags -%}
          <li><a class="tag" href="{{ questions_url(tagged=tags_to_text
                  (tags|remove(tag))) }}">{{ tag.name }}</a></li>
          {%- endfor %}
            </ul>
          <a class="show-all" href="{{ questions_url(tagged=None) }}">{{ _('Show all questions') }}</a>
        </div>
      {% endif %}
    </div>

    <div class="grid_9">
      <div id="owner-tabs" class="cf">
        <a href="{{ questions_url(show='needs-attention', filter=None, page=None) }}" {{ show|class_selected('needs-attention') }}>
          {% include "includes/icons/alert-icon.svg" %}
          {{ _('Attention needed') }}
        </a>
        <a href="{{ questions_url(show='responded', filter=None, page=None) }}" {{ show|class_selected('responded') }}>
          {% include "includes/icons/message-icon.svg" %}
          {{ _('Responded') }}
        </a>
        <a href="{{ questions_url(show='done', filter=None, page=None) }}" {{ show|class_selected('done') }}>
          {% include "includes/icons/check-icon.svg" %}
          {{ _('Done') }}
        </a>
        <a href="{{ questions_url(show='all', filter=None, page=None) }}" {{ show|class_selected('all') }}>
          {% include "includes/icons/list-icon.svg" %}
          {{ _('All questions') }}
        </a>
      </div>
    </div>

    <div class="grid_3">
      <section id="top-contributors">
        {% set query_product = '?product={}'.format(products[0].slug) if products and products|length == 1 else '' %}
        <a class="feature-box" href="{{ url('community.top_contributors', area='questions') }}{{ query_product }}">
          {{ _('Top Contributors') }}
        </a>
      </section>
    </div>
  </div>

  <div id="filter-section"></div>

  <div id="questions-list" class="container_12 cf">
    <div class="grid_9">
      {% if questions.object_list %}
        <article class="questions">
          {% for question in questions.object_list %}
            <section id="question-{{ question.id }}" class="cf">
              <div class="question-meta {% if not question.num_answers %}urgent{% endif %}">
                <div class="replies">
                  <h4>{{ question.num_answers }}</h4>
                  {% trans count=question.num_answers %}
                    reply
                    {% pluralize %}
                    replies
                  {% endtrans %}
                </div>
                <div class="have-problem-this-week">
                  <h4>{{ question.num_votes_past_week }}</h4>
                  {{ _('votes this week') }}
                </div>
                {% if question.num_visits %}
                  <div class="views">
                    <h4>{{ question.num_visits }}</h4>
                    {% trans count=question.num_visits %}
                      view
                      {% pluralize %}
                      views
                    {% endtrans %}
                  </div>
                {% endif %}
              </div>

              <div class="thread-meta">
                <div class="thread-solved {% if question.is_solved %}highlighted{% endif %}">
                  {{ _('Solved') }}
                </div>
                <div class="thread-contributed {% if question.is_contributor(user) %}highlighted{% endif %}">
                  {{ _('Contributed') }}
                </div>
                {% if question.is_locked %}
                  <div class="thread-locked highlighted">{{ _('Locked') }}</div>
                {% endif %}
                {% if question.is_archived %}
                  <div class="thread-archived highlighted">{{ _('Archived') }}</div>
                {% endif %}
              </div>

              <div class="content ui-truncatable truncated">
                <h2><a href="{{ question.get_absolute_url() }}">{{ question.title }}</a></h2>
                {{ question.content_parsed|truncate_question(170, question.content_parsed)|safe }}

                <div class="user-meta cf">
                  <div class="user-meta-asked-by">
                    {{ _('asked by <a href="{user_url}">{username}</a> {when}')|fe(user_url=profile_url(question.creator), username=display_name(question.creator), when=question.created|timesince) }}
                  </div>
                  {% if question.solution %}
                    <div class="user-meta-answered-by">
                      {{ _('answered by <a href="{user_url}">{username}</a> {when}')|fe(user_url=profile_url(question.solution.creator), username=display_name(question.solution.creator), when=question.solution.created|timesince) }}
                    </div>
                  {% else %}
                    {% if question.last_answer %}
                      <div class="user-meta-answered-by">
                        {{ _('<a href="{reply_url}">last reply</a> by <a href="{user_url}">{username}</a> {when}')|fe(reply_url=question.last_answer.get_absolute_url(), user_url=profile_url(question.last_answer.creator), username=display_name(question.last_answer.creator), when=question.last_answer.created|timesince) }}
                      </div>
                    {% endif %}
                  {% endif %}
                </div>

                {% set tags = question.my_tags %}
                <ul class="tag-list">
                {% if question.topic %}
                  {% set topic = question.topic %}
                  <li>
                    <a href="{{ url('questions.list', topic.product.slug)|urlparams(None, request.GET, topic=None) }}">
                      <img src="{{ topic.product.image_alternate_url }}" class="logo-sprite-tiny">
                      {{ pgettext('DB: products.Product.title', topic.product.title) }}
                    </a>
                  </li>
                {% endif %}
                {% for tag in tags %}
                  <li><a class="tag-name" href="{{ questions_url(tagged=tag.slug) }}">{{ tag }}</a></li>
                {% endfor %}
                </ul>
              </div>
            </section>
          {% endfor %}
        </article>
        {{ questions|quick_paginator }}
      {% else %}
        <p>{{ _('There are no questions that match the current filter settings.') }}</p>
      {% endif %}
    </div>

    <div class="grid_3 questions-sidebar">
      <div class="sidebar-header">{{ _('Filter by') }}</div>
      <ul class="sidebar-nav">
        <li {% if filter == None and escalated == None %}class="selected"{% endif %}><a href="{{ questions_url(filter=None, escalated=None, page=None) }}">{{ _('All') }}</a></li>
        {% for f, desc in filters.iteritems() %}
          <li {{ filter|class_selected(f) }}><a href="{{ questions_url(filter=f, escalated=None, page=None) }}">{{ desc }}</a></li>
        {% endfor %}
        <li {% if filter == None and escalated %}class="selected"{% endif %}><a href="{{ questions_url(escalated=1, filter=None, page=None) }}">{{ _('Escalated') }}</a></li>
      </ul>

      <div class="sidebar-header">{{ _('Sort options') }}</div>
      <ul class="sidebar-nav">
        {% for o, details in orders.iteritems() %}
          <li {{ order|class_selected(o) }}><a {% if order == o %}class="sort-{{ sort }}"{% endif %} href="{{ questions_url(order=o, sort=None if order != o else 'desc' if sort == 'asc' else 'asc', page=None) }}">{{ details[1] }}</a></li>
        {% endfor %}
      </ul>

      <div class="sidebar-header">{{ _('Show me') }}</div>
      <ul class="sidebar-nav">
        <li {{ owner|class_selected(None) }}><a href="{{ questions_url(owner='all', page=None) }}">{{ _('Posts from everyone') }}</a></li>
        {% if request.user.is_authenticated() %}
          <li {{ owner|class_selected('mine') }}><a href="{{ questions_url(owner='mine', page=None) }}">{{ _('My contributions') }}</a></li>
        {% endif %}
      </ul>

      <div class="toggler">
        <span class="expand" data-toggle="collapsed" data-toggle-target="#questions-list" data-toggle-sticky="sticky"></span>
        <span class="collapse" data-toggle="collapsed" data-toggle-target="#questions-list" data-toggle-sticky="sticky"></span>
      </div>
    </div>
  </div>
{% endblock %}

{% block side %}
{% endblock %}
